package com.shop.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shop.entity.User;
import com.shop.service.UserService;

/**
 * Servlet implementation class DoRegister
 */
@WebServlet("/doRegister")
public class DoRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DoRegister() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	private UserService UserService = new UserService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String msg = "";
		// 获取 用户的用户名和密码
		User user = new User();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String password2 = request.getParameter("password2");
		
		
		// 判断 用户的用户名和密码是否填写
		if (!username.equals("") && !password.equals("") && !password2.equals("") && password.equals(password2)){
			user.setUsername(username);
			user.setPassword(password);
			user.setPassword(password2);
			UserService.addUser(user);
			request.setAttribute("msg", "注册成功,请登录");
			request.getRequestDispatcher("login.jsp").forward(request, response);
			return;
			
		}else{
			request.setAttribute("msg", "注册失败,请输入完整的信息或账号已经存在");
			request.getRequestDispatcher("register.jsp").forward(request, response);
			return;
		}
		
	}
		
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
